const request = require('request');
const pinyin = require("pinyin");

module.exports = {
    randArray(array) {
        let index = Math.floor((Math.random()*array.length)); 
        return(array[index]); 
    },

    uget(url) {
        return new Promise(function (reslove, reject) {
            let rOpts = {
                url: url,
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
                    'Upgrade-Insecure-Requests' : '1',
                    'Pragma' : 'no-cache',
                    'Cache-Control' : 'no-cache',
                    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
                }
            }
            request(rOpts, function (error, response, body) {
                reslove({
                    error: error,
                    response: response,
                    body: body
                })
            });
        })
    },

    topy(str) {
        let tempArr = pinyin(str, {
            style: pinyin.STYLE_NORMAL, // 设置拼音风格
        });
        let tempStr = "";
        tempArr.forEach(function(item) {
            tempStr+=item[0];
        });
        return tempStr;
    },

    randomNum(minNum, maxNum) {
        switch(arguments.length){ 
            case 1: 
                return parseInt(Math.random()*minNum+1,10); 
            break; 
            case 2: 
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
            break; 
                default: 
                    return 0; 
                break; 
        } 
    },

    sleep(second) {
        return new Promise(function (reslove, reject) {
            setTimeout(function() {
                reslove(true);
            }, second * 1000);
        });
    }
}